<div id="formDiv"></div>
<script type="text/html" id="shipmentOrderSignForm">
    <form lay-filter="SignFormContent" id="SignFormContent" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <div>
                    <table class="layui-table">
                        <thead>
                        <tr>
                            {{# if(d.eventName === 'detail'){ }}
                            <th lay-data="{field:'shipmentOrderCode', width:150, sort: true, fixed: 'left'}">
                                发货记录编号
                            </th>
                            {{# } }}
                            {{# if(d.eventName !== 'detail'){ }}
                            <th lay-data="{field:'shipmentOrderCode', width:150, sort: true, fixed: 'left'}">
                                选择发货运单号
                            </th>
                            {{# } }}
                            <th lay-data="{field:'materialName', width:150, sort: true, fixed: 'left'}">数据来源
                            </th>
                            <th lay-data="{field:'materialSpec', width:150, sort: true, fixed: 'left'}">所属合同编号</th>
                            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">客户代码</th>
                            {{# if(d.eventName === 'detail'){ }}
                            <th lay-data="{field:'materialSpec', width:150, sort: true, fixed: 'left'}">
                                物料总净重
                            </th>
                            <th lay-data="{field:'materialSpec', width:150, sort: true, fixed: 'left'}">运单号</th>
                            {{# } }}
                            {{# if(d.eventName !== 'detail'){ }}
                            <th lay-data="{field:'materialSpec', width:150, sort: true, fixed: 'left'}">
                                物料重量
                            </th>
                            <th lay-data="{field:'materialSpec', width:150, sort: true, fixed: 'left'}">发货记录编号</th>
                            {{# } }}
                            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">车牌号</th>
                            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">司机</th>
                            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">司机电话</th>
                            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">状态
                            </th>
                            {{# if(d.eventName !== 'detail'){ }}
                            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">签收时间
                            </th>
                            {{# } }}
                            {{# if(d.eventName === 'detail'){ }}
                            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">创建时间
                            </th>
                            {{# } }}
                            {{# if(d.eventName !== 'detail'){ }}
                            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">签收人
                            </th>
                            {{# } }}
                            {{# if(d.eventName === 'detail'){ }}
                            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">创建人
                            </th>
                            {{# } }}
                            {{# if(d.eventName === 'sign'){ }}
                            <th lay-data="{field:'materialUnit', width:150, sort: true, fixed: 'left'}">入库仓库
                            </th>
                            {{# } }}

                        </tr>
                        </thead>
                        <tbody>
                        <tr>

                            <td class="chooseCode">
                                {{# if(d.eventName !== 'detail'){ }}
                                <select id="waybillI" name="waybillI"
                                        lay-filter="waybillI" lay-search required></select>

                                {{# } }}
                                {{# if(d.eventName === 'detail'){ }}
                                <div  style="width: 100%;height: 100%;display: flex;align-items: center;justify-content: center;text-align: center"  id="shipmentOrderCode" ></div>
                                <div class="mask"></div>
                                {{# } }}
                            </td>

                            <td>
                                <input class="layui-input adv-disable" id="source" disabled
                                       name="source"/>
                            </td>
                            <td>
                                <input type="text" class="layui-input" disabled id="contractCode"
                                       name="contractCode"/>
                            </td>
                            <td>
                                <input type="text" class="layui-input" disabled id="warehouseCustomerId"
                                       name="warehouseCustomerId"/>
                            </td>
                            <td>
                                <input class="layui-input adv-disable" id="totalWeight" disabled
                                       name="totalWeight"/>
                            </td>
                            {{# if(d.eventName === 'detail'){ }}
                            <td>
                                <input class="layui-input adv-disable" id="waybill" disabled
                                       name="waybill"/>
                            </td>
                            {{# } }}
                            {{# if(d.eventName !== 'detail'){ }}
                            <td>
                                <input class="layui-input adv-disable" id="shipmentOrderCodeID" disabled
                                       name="shipmentOrderCode"/>
                            </td>
                            {{# } }}

                            <td>
                                <input type="text" class="layui-input" disabled name="plateNo"/>
                            </td>

                            <td>
                                <input type="text" class="layui-input" disabled name="driverName"/>
                            </td>
                            <td>
                                <input type="text" class="layui-input" disabled name="driverPhone"/>
                            </td>
                            <td>
                                <input type="text" class="layui-input" disabled name="shipmentOrderStatusName"/>
                            </td>
                            <td>
                                <input class="layui-input adv-disable" id="createTime" disabled name="createTime"/>
                            </td>
                            <td>
                                <input class="layui-input adv-disable" id="creatorName" disabled
                                       name="creatorName"/>
                            </td>

                            {{# if(d.eventName === 'sign'){ }}
                            <td class="chooseCode">
                                <select id="warehouseId" name="warehouseId"
                                        lay-filter="warehouseId" lay-search required></select>
                            </td>
                            {{# } }}
                        </tr>
                        </tbody>
                    </table>
                </div>
                <!--      <div class="layui-form-item tableContent">-->
                <!--        <table class="layui-table" id="signFormtable" lay-filter="signFormtable"></table>-->
                <!--      </div>-->
                <div class="layui-form-item tableContent" id="signC">
                    <table class="layui-table" id="signCheckFormTable" lay-filter="signCheckFormTable"></table>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                {{# if(d.eventName !== 'detail'){ }}
                <button class="layui-btn" lay-filter="signcheckFormSubmit" type="button" lay-submit id="formSubmitUp">批量签收</button>
                {{# } }}
            </div>
        </div>
    </form>
</script>
<!--签收操作-->
<script type="text/html" id="signcheckFormTableBar">
    <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="signClick" type="button">签收</button>
</script>
<script>
    layui.use(["layer", "form", "table", "admin", "laydate", "upload", "laytpl"], async function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        let laydate = layui.laydate;
        let laytpl = layui.laytpl;
        const nowData = { ...parentData };
        let data = nowData?.data;
        let waybill = nowData?.waybill;
        let skuList = [];
        var formData={}
        var warehouseAreaList=[]
        const typeList=[
            {
                val:1,name:'普通物资'
            },{
                val:2,name:'加工预料'
            },{
                val:3,name:'加工原料'
            },{
                val:4,name:'加工成品'
            }
        ]
        const signQualifiedList=[
            {
                val:1,name:'合格'
            },{
                val:0,name:'不合格'
            }
        ]
        const statusList=[
            {val:1,name:'待签收'},
            {val:2,name:'已签收'},
            {val:3,name:'已入库'},
            {val:0,name:'已撤销'},
        ]

        laytpl(shipmentOrderSignForm.innerHTML).render(nowData, function (html) {
            $("#formDiv").after(html);
        });
        if (nowData.eventName === "detail") {
            admin.setFormDisable("#SignFormContent", true);
            form.render();
            $("#shipmentOrderCode").html(nowData.data.shipmentOrderCode);
        }

        console.log(waybill,"12")

        await admin.initDataSelectPro(
            "warehouse/warehouse/shipmentOrder/list",
            {limit: 99999},
            "waybillI",
            "发货运单号",
            async (d, obj) => {
                var item = {
                    shipmentOrderCode: "",
                    source: '',
                    contractCode: '',
                    warehouseCustomerId : '',
                    totalWeight : '',
                    plateNo: '',
                    waybill : '',
                    driverName  : '',
                    driverPhone: '',
                    shipmentOrderStatus: '',
                    createTime: '',
                    creatorName: ''
                }
                form.val("SignFormContent", item);
                // 清空排产信息列表
                table.reload("signCheckFormTable", {
                    data: []
                });
                if (obj) {

                }
                if (d.value) {
                    let kData = await admin.reqPro("warehouse/warehouse/signOrder/addInfo", {waybill: d.value});
                    let shipmentOrderCodeData=kData.data
                    if(shipmentOrderCodeData.source==2){
                        shipmentOrderCodeData.source='管理后台'
                    }else if(shipmentOrderCodeData.source==1){
                        shipmentOrderCodeData.source='客户端'
                    }
                    let info=statusList.filter(item=>item.val==shipmentOrderCodeData.shipmentOrderStatus)
                    if(info.length){
                        shipmentOrderCodeData.shipmentOrderStatusName=info[0].name
                    }
                    let shipmentOrderDetailInfoList=shipmentOrderCodeData.orderInfoDetail
                    shipmentOrderDetailInfoList.map(item=>item.signQualified=1)
                    formData=shipmentOrderCodeData
                    table.reload("signCheckFormTable", {
                        data: shipmentOrderDetailInfoList
                    });
                    form.val("SignFormContent", shipmentOrderCodeData);
                } else {
                }

            },
            "waybill","waybill"
        );




        table.on("tool(signCheckFormTable)", function (obj) {
            var d = obj.data; // 获取当前行的数据
            var layEvent = obj.event; // 获取事件名（edit 或 del）
            if (obj.tr) {
                obj.tr.on("click", function (e) {
                    e.preventDefault(); // 防止默认行为
                });
            }
            if (layEvent == "signClick") {
                console.log(d,"签收点击")
            }


        });

        const tableVal = table.render({
            elem: '#signCheckFormTable',
            page: false,
            limit: 999999,
            height: 240,
            data: [],
            cols: [[
                {type: 'checkbox', fixed: 'left',
                    hide: nowData.eventName === "detail" ? true : false
                },
                {type: "numbers", title: "序号"},
                {field: 'userBundleDetail', title: '客户捆包号', align: 'center'}
                , {field: 'productSpecCode', title: '规格码', align: 'center'}
                , {field: 'productMark', title: '钢种/牌号', align: 'center'}
                , {field: 'productProducer', title: '产地', align: 'center'}
                , {field: 'productVariety', title: '品种', align: 'center'}
                , {field: 'productUnit', title: '计量单位', align: 'center'}
                , {field: 'totalQuantity', title: '数量', align: 'center'}
                , {
                    field: 'grossWeight', title: '毛重(吨)', align: 'center',
                    // templet: admin.setTableInput("grossWeight", nowData.eventName === "detail", "grossWeight")
                }
                , {
                    field: 'netWeight', title: '净重(吨)', align: 'center',
                    // templet: admin.setTableInput("netWeight", nowData.eventName === "detail", "netWeight")
                }
                , {
                    field: 'productType', title: '物资类型', align: 'center',
                    hide: nowData.eventName === "detail" ? false : true,
                    templet: admin.setTableSelect("productType",
                        typeList.map((v) => {
                            return {
                                label: v.name,
                                value: v.val
                            };
                        }), nowData.eventName === "detail", "productType")
                }, {
                    field: 'signQualified', title: '是否合格', align: 'center',
                    hide: nowData.eventName === "detail" ? true : false,
                    templet: admin.setTableSelect("signQualified",
                        signQualifiedList.map((v) => {
                            return {
                                label: v.name,
                                value: v.val
                            };
                        }), nowData.eventName === "detail", "signQualified")
                },{
                    field: 'warehouseAreaId', title: '库区', align: 'center',
                    hide: nowData.eventName === "detail" ? true : false,
                    templet: function (d) {
                        if (d.warehouseAreaList) {
                            return admin.setTableSelect(
                                "warehouseAreaId",
                                d.warehouseAreaList.map((v) => {
                                    return {
                                        label: v.name,
                                        value: v.id
                                    };
                                }),
                                nowData.eventName === "detail",
                                "warehouseAreaId"
                            )(d);
                        } else {
                            return "";
                        }
                    }
                }
                ,{
                    field: 'warehouseShelfSpaceId', title: '库位', align: 'center',
                    hide: nowData.eventName === "detail" ? true : false,
                    templet: function (d) {
                        if (d.codeList) {
                            return admin.setTableSelect(
                                "warehouseShelfSpaceId",
                                d.codeList.map((v) => {
                                    return {
                                        label: v.code,
                                        value: v.id
                                    };
                                }),
                                nowData.eventName === "detail",
                                "warehouseShelfSpaceId"
                            )(d);
                        }else{
                            return ''
                        }
                    }
                }
                , {
                    field: 'remarks', title: '备注', align: 'center',
                    templet: admin.setTableInput("remarks", nowData.eventName === "detail", "remarks")
                }
                // , {
                //     align: 'center',
                //     toolbar: '#signcheckFormTableBar',
                //     title: '操作',
                //     width: 100,
                //     fixed: 'right',
                //     hide: nowData.eventName === "detail" ? true : false
                // }
            ]],
            done: function (res, page, count) {

                $('.layui-table-header input[name="layTableCheckbox"]').prop('checked', true)
                $('.layui-table-header input[name="layTableCheckbox"]').next().addClass('layui-form-checked')
                res.data.forEach((element, index) => {
                    element["LAY_CHECKED"]='true'
                    $('tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
                    $('tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
                });
                admin.tableInputChange("signCheckFormTable");
                admin.tableSelectChange("signCheckFormTable", 'signQualified');
                admin.tableSelectChange("signCheckFormTable", 'warehouseShelfSpaceId');
                admin.tableSelectChange("signCheckFormTable", "warehouseAreaId", (index, value, tableData, field) => {
                    admin.req(
                        "warehouse/warehouse/admin/warehouseFactoryShelfSpace/list",
                        {
                            warehouseAreaId: value
                        },
                        (res) => {
                            tableData[index].codeList = res.data;
                            table.reload("signCheckFormTable", {
                                data: tableData
                            });

                        }
                    );
                });
            },
        });


        /*直接从签收进来*/
        if(waybill){
            let kData = await admin.reqPro("warehouse/warehouse/signOrder/addInfo", {waybill: waybill});
            let shipmentOrderCodeData=kData.data
            if(shipmentOrderCodeData.source==2){
                shipmentOrderCodeData.source='管理后台'
            }else if(shipmentOrderCodeData.source==1){
                shipmentOrderCodeData.source='客户端'
            }
            let info=statusList.filter(item=>item.val==shipmentOrderCodeData.shipmentOrderStatus)
            if(info.length){
                shipmentOrderCodeData.shipmentOrderStatusName=info[0].name
            }
            let shipmentOrderDetailInfoList=shipmentOrderCodeData.orderInfoDetail
            shipmentOrderDetailInfoList.map(item=>item.signQualified=1)
            formData=shipmentOrderCodeData
            $("#waybillI").val(waybill)
            table.reload("signCheckFormTable", {
                data: shipmentOrderDetailInfoList
            });
            form.val("SignFormContent", shipmentOrderCodeData);


            await admin.initDataSelectPro(
                "warehouse/warehouse/admin/administration/list",
                {limit: 99999},
                "warehouseId",
                "入库仓库",
                async (d, obj) => {
                    if (d.value) {
                        let allTableCont=table.cache["signCheckFormTable"];
                        admin.req(
                            "warehouse/warehouse/admin/warehouseFactoryArea/list",
                            {
                                warehouseId: d.value
                            },
                            (res) => {
                                allTableCont.forEach(item=>{
                                    item.warehouseAreaList=res.data
                                })
                                tableVal.reload()
                            }
                        );
                    }
                },
                "name"
            );

        }


        if (data && data.id) {
            if(data.source==2){
                data.source='管理后台'
            }else if(data.source==1){
                data.source='客户端'
            }
            let info=statusList.filter(item=>item.val==data.shipmentOrderStatus)
            if(info.length){
                data.shipmentOrderStatusName=info[0].name
            }
            form.val("SignFormContent", data);
            tableVal.reload({
                data: data.orderInfoDetail.map((v) => {
                    v.eventName = "detail";
                    return v;
                })
            });
        } else {
        }

        //主框确定
        form.on("submit(signcheckFormSubmit)", function (d) {
            let allTableCont=table.cache["signCheckFormTable"];
            var checkRows = table.checkStatus('signCheckFormTable');
            var warehouseSignInventory=checkRows.data
            allTableCont.map((item)=>{
                item.warehouseId=$("#warehouseId").val()
                item.id=item.warehouseProductInventoryId
                if(item.LAY_CHECKED){
                    item.isSign='1'
                }else{
                    item.isSign='0'
                    item.warehouseAreaId=''
                    item.warehouseShelfSpaceId=''
                }
            })
            if (warehouseSignInventory.length == 0) {
                layer.msg("请选择需要签收的数据");
                return false
            }
            if (warehouseSignInventory.some(item =>item.signQualified === '')) {
                layer.msg("请选择是否合格", {icon: 2, time: 1000});
                return false;
            }
            if (warehouseSignInventory.some(item =>item.warehouseAreaId == undefined||item.warehouseAreaId == '')) {
                layer.msg("请选择库区", {icon: 2, time: 1000});
                return false;
            }
            if (warehouseSignInventory.some(item =>item.warehouseShelfSpaceId==undefined||item.warehouseShelfSpaceId == '')) {
                layer.msg("请选择库位", {icon: 2, time: 1000});
                return false;
            }
            // warehouseSignInventory.map(item=>{item.id=item.warehouseProductInventoryId})
            formData.totalQuantity=formData.totalWeight
            formData.source=2
            formData.warehouseId=$("#warehouseId").val()
            formData.shipmentOrderId=formData.id
            formData.orderCode=formData.contractCode
            formData.customerId=formData.warehouseCustomerId
            formData.totalQuantity=formData.totalWeight
            formData.warehouseSignInventory=allTableCont
            admin.req('warehouse/warehouse/signOrder/add',  JSON.stringify(formData), function (res) {
                layer.closeAll('loading');
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    table.reload("warehouseShipmentOrderTable", {});
                    layer.closeAll("page");
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, "POST",
                false,
                "application/json");

            return false;
        });

        $("#warehouseProductionOrderForm .layui-table-body").css("overflow", "visible");
        $("#warehouseProductionOrderForm .layui-table-box").css("overflow", "visible");
        $("#warehouseProductionOrderForm .layui-table-view").css("overflow", "visible");
    });
</script>

<style>
    #warehouseProductionOrderForm .layui-form-label {
        width: 125px;
    }

    #warehouseProductionOrderForm .layui-input-block {
        margin-left: 155px;
    }

    #warehouseProductionOrderForm .layui-form-item .layui-inline {
        /* margin-bottom: 5px; */
        margin-right: 0px;
    }

    #warehouseProductionOrderForm .layui-table-cell {
        overflow: visible !important;
    }

    .chooseCode {
        position: relative;
    }

    .mask {
        position: absolute;
        width: 100%;
        height: 100%;
        background: rgba(158, 157, 157, 0.1);
        top: 50%;
        left: 50%;
        z-index: 2;
        transform: translate(-50%, -50%);
    }
</style>